//
//  LKChangePasswordVC.m
//  TJHYHardwareProduct
//
//  Created by Kendama on 2018/1/22.
//  Copyright © 2018年 Kendama. All rights reserved.
//

#import "LKChangePasswordVC.h"
#import "LKCustonTextFieldView.h"
#import "HYEditUserApi.h"

@interface LKChangePasswordVC ()
@property(nonatomic,strong)LKCustonTextFieldView *oldPasswordView;
@property(nonatomic,strong)LKCustonTextFieldView *passwordView;
@property(nonatomic,strong)LKCustonTextFieldView *confirmPasswordView;
@property(nonatomic,strong)UIButton *commitButton;
@end

@implementation LKChangePasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

#pragma mark --- 点击事件
- (void)didClickCommitButton
{
    NSString *old = self.oldPasswordView.textField.text;
    NSString *password = self.passwordView.textField.text;
    NSString *confirmPassword = self.confirmPasswordView.textField.text;

    if (![old isRegexPassword])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的密码"];
        return;
    }
    
    if (![password isRegexPassword])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的密码"];
        return;
    }
    
    if (![confirmPassword isRegexPassword])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的密码"];
        return;
    }
    
    if (![password isEqualToString:confirmPassword]) {
        [SVProgressHUD showErrorWithStatus:@"两次密码不一致"];
        return;
    }
    
    
    
    [HYEditUserApi editPhone:nil old_password:old password:password com_password:confirmPassword success:^(NSDictionary *dic) {
        [SVProgressHUD showSuccessWithStatus:@"修改成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            SAVE_OBJECT(@"NO", LOGIN);
            [[AppDelegate defaultService] setRootViewController];
//            [self.navigationController popViewControllerAnimated:YES];
        });
    } fail:^(NSDictionary *dic) {
        [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
    }];
}

#pragma mark --- 懒加载
- (LKCustonTextFieldView *)oldPasswordView
{
    if(!_oldPasswordView)
    {
        UIView *superView = self.view;
        _oldPasswordView = [[LKCustonTextFieldView alloc]init];
        _oldPasswordView.textField.placeholder = @"请输入旧密码";
        _oldPasswordView.textField.secureTextEntry = YES;
        [superView addSubview:_oldPasswordView];
    }
    return _oldPasswordView;
}

- (LKCustonTextFieldView *)passwordView
{
    if(!_passwordView)
    {
        UIView *superView = self.view;
        _passwordView = [[LKCustonTextFieldView alloc]init];
        _passwordView.textField.placeholder = @"请输入新密码";
        _passwordView.textField.secureTextEntry = YES;
        [superView addSubview:_passwordView];
    }
    return _passwordView;
}
- (LKCustonTextFieldView *)confirmPasswordView
{
    if(!_confirmPasswordView)
    {
        UIView *superView = self.view;
        _confirmPasswordView = [[LKCustonTextFieldView alloc]init];
        _confirmPasswordView.textField.placeholder = @"请确认新密码";
        _confirmPasswordView.textField.secureTextEntry = YES;
        [superView addSubview:_confirmPasswordView];
    }
    return _confirmPasswordView;
}
- (UIButton *)commitButton
{
    if(!_commitButton)
    {
        UIView *superView = self.view;
        _commitButton = [[UIButton alloc]init];
        [superView addSubview:_commitButton];
        [_commitButton setTitle:@"确定" forState:UIControlStateNormal];
        [_commitButton.titleLabel setFont:fontWithSize(15)];
        [_commitButton setBackgroundImage:[UIImage imageNamed:@"button_login_nor"] forState:UIControlStateNormal];
        [_commitButton addTarget:self action:@selector(didClickCommitButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _commitButton;
}

#pragma mark --- 初始化布局
- (void)base_masLayoutSubview
{
    __weak UIView *superView = self.view;
    
    [self.oldPasswordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64+px1920Height(40));
        make.left.right.mas_equalTo(superView);
        make.height.mas_equalTo(px1920Height(136));
    }];
    
    [self.passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_oldPasswordView.mas_bottom).with.offset(px1920Height(30));
        make.left.right.height.mas_equalTo(_oldPasswordView);
    }];
    
    [self.confirmPasswordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_passwordView.mas_bottom).with.offset(px1920Height(30));
        make.left.right.height.mas_equalTo(_oldPasswordView);
    }];
    
    [self.commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_confirmPasswordView.mas_bottom).with.offset(px1920Height(147));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(66));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(66));
        make.height.mas_equalTo(px1920Height(136));
    }];
    
    superView.backgroundColor = rgba(245,245,245,1);
}

#pragma mark --- 导航栏
- (void)base_configNavigationBar
{
    [self setNavigationTitle:@"修改密码" titleColor:[UIColor blackColor]];
    [self setLeftButtonImage:[UIImage imageNamed:@"left"]];
    self.navigationBar.backgroundColor = [UIColor whiteColor];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
